package spring.boot.admin.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

@Data
@TableName("sys_menu")
public class SysMenu implements GrantedAuthority {

    @TableId(type = IdType.AUTO)
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    @JsonSerialize(using = ToStringSerializer.class)
    private Long pid;

    private String name;

    private String url;

    private String authority;

    private Integer enable;

    private Integer sort;

    @TableField("create_time")
    private String createTime;


    /* 以下为非数据库字段 */
    @TableField(exist = false)
    private String state = "closed";

    @TableField(exist = false)
    private List<SysMenu> children = new ArrayList<>();

    @TableField(exist = false)
    private Boolean checked = Boolean.FALSE;

    public String getCreateTimeString() {
        if (createTime != null) {
            long time = Long.parseLong(createTime);
            LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(time / 1000, 0, ZoneOffset.ofHours(8));
            return localDateTime.toString().replace("T", " ");
        } else {
            return null;
        }
    }


    /* 以下为字段在 jquery easyui 中定义好的需要转换的名字 */
    public String getText() {
        return name;
    }

    public Long getParentId() {
        return pid;
    }


}
